﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Data;

namespace OneMile.Common
{
    public static class ExtensionMethods
    {

        #region 值与字符操作

        /// <summary>
        /// 去掉字符串最后一个字符
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string CutLastChar(this String str)
        {
            if (str.Length > 0)
            {
                return str.Substring(0, str.Length - 1);
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 转换成int32类型
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int TInt32(this Object obj)
        {
            try
            {
                if (obj == null)
                {
                    return 0;
                }
                if (obj == DBNull.Value)
                {
                    return 0;
                }
                 
                return Convert.ToInt32(obj);
                
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 转换成int32类型
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int TInt32(this String obj)
        {
            try
            {
                if (String.IsNullOrEmpty(obj))
                {
                    return 0;
                }
                return Convert.ToInt32(obj);

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 某列的值转换成int32类型
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="data">列名</param>
        /// <returns>NULL则为0</returns>
        public static int TInt32(this DataRow dr, string ColumnName)
        {
            try
            {
                if (dr == null)
                {
                    return 0;
                }
                else
                {
                    return Convert.ToInt32(dr[ColumnName]);
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public static Guid TGuid(this String obj)
        {
            try
            {
                if (String.IsNullOrEmpty(obj))
                {
                    return Guid.Empty;
                }
                else
                {
                    return new Guid(obj);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static Guid TGuid(this Object obj)
        {
            try
            {
                if (obj == null)
                {
                    return Guid.Empty;
                }
                if (obj == DBNull.Value)
                {
                    return Guid.Empty; ;
                }
                else
                {
                    return new Guid(Convert.ToString(obj));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 转换成String类型
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string TString(this Object obj)
        {

            try
            {
                if (obj == null)
                {
                    return "";
                }
                if (obj == DBNull.Value)
                {
                    return "";
                }
                
                return Convert.ToString(obj);
                
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 转换成String类型
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string TString(this Decimal obj,string str)
        {

            try
            {
                if (obj == null)
                {
                    return "";
                }
                return obj.ToString(str);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 转换成String类型
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string TString(this Double obj, string str)
        {

            try
            {
                if (obj == null)
                {
                    return "";
                }
                return obj.ToString(str);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 转换成DateTime类型
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static DateTime TDateTime(this Object obj)
        {
            try
            {
                if (obj == null)
                {
                    return Convert.ToDateTime("2000/01/01 00:00:00");
                }
                if (obj == DBNull.Value)
                {
                    return Convert.ToDateTime("2000/01/01 00:00:00");
                }
                
                return Convert.ToDateTime(obj);
                
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// SQL的Time类型转换成DateTime类型
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static DateTime TTime(this Object obj)
        {
            try
            {
                if (obj == null)
                {
                    return Convert.ToDateTime("2000/01/01 00:00:00");
                }
                if (obj == DBNull.Value)
                {
                    return Convert.ToDateTime("2000/01/01 00:00:00");
                }

                return DateTime.ParseExact(obj.ToString(),"HH:mm:ss", null);

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 转换成只包含日期时间的字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string TDateTimeString(this DateTime obj)
        {
            try
            {
                if (obj == null)
                {
                    return "";
                }

                return obj.ToString("yyyy-MM-dd HH:mm:ss");

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 转换成只包含时间的字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string TTimeString(this DateTime obj)
        {
            try
            {
                if (obj == null)
                {
                    return "";
                }

                return obj.ToString("t");

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }


        /// <summary>
        /// 转换成Double类型
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static double TDouble(this Object obj)
        {
            try
            {
                if (obj == null)
                {
                    return 0;
                }
                if (obj == DBNull.Value)
                {
                    return 0;
                }

                return Convert.ToDouble(obj);

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 转换成Decimal类型
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static decimal TDecimal(this Object obj)
        {

            try
            {
                if (obj == null)
                {
                    return 0;
                }
                if (obj == DBNull.Value)
                {
                    return 0;
                }
                
                return Convert.ToDecimal(obj);
                
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }


        /// <summary>
        /// 某列的值转换成double类型
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="data">列名</param>
        /// <returns>NULL则为0</returns>
        public static double TDouble(this DataRow dr, string ColumnName)
        {

            try
            {
                if (dr == null)
                {
                    return 0;
                }
                else
                {
                    return Convert.ToDouble(dr[ColumnName]);
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 四舍五入
        /// </summary>
        /// <param name="db"></param>
        /// <returns></returns>
        public static double Round(this double db)
        {
            if (db < 0)
            {
                db = Math.Round(db + 5 / Math.Pow(10, 3), 2, MidpointRounding.AwayFromZero);
            }
            else
            {
                db = Math.Round(Convert.ToDouble(db), 2, MidpointRounding.AwayFromZero);
            }

            return db;
        }


        #endregion


        #region 控件
        public static void Sort<TSource, TValue>(this List<TSource> source, Func<TSource, TValue> selector)
        {
            var comparer = Comparer<TValue>.Default;
            source.Sort((x, y) => comparer.Compare(selector(x), selector(y)));
        }
        public static void SortDescending<TSource, TValue>(this List<TSource> source,Func<TSource, TValue> selector)
        {
            var comparer = Comparer<TValue>.Default;
            source.Sort((x, y) => comparer.Compare(selector(y), selector(x)));
        }



        #endregion


        #region XML操作

        public static string FindValue(this XmlNodeList xnl, KeyValuePair<string, string> KnownProperty, string ReturnPropertyName)
        {
            foreach (XmlNode xn in xnl)
            {
                if (xn.Attributes[KnownProperty.Key].Value == KnownProperty.Value)
                {
                    if (ReturnPropertyName == "InnerText")
                    {
                        return xn.InnerText;
                    }
                    else
                    {
                        return xn.Attributes[ReturnPropertyName].Value;
                    }
                }
            }
            return KnownProperty.Value;
        }

        #endregion
    }
}
